package jmine.tec.report.impl.table.xls;

import jmine.tec.report.api.table.ReportCell;
import jmine.tec.report.impl.table.ReportTableBuilder;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 
 * @author adalberto.kishi
 *
 */
public class NumberPorcentagemCellXLSRenderer extends NumberCellXLSRenderer {

    /**
     * {@inheritDoc}
     */
    public String getRendererType() {
        return ReportTableBuilder.TYPE_PORCENTAGEM;
    }
    
    /**
     * {@inheritDoc}
     */
    public void render(HSSFWorkbook workbook, HSSFCell hssfCell, ReportCell cell) {
        if (cell.getValue() != null) {
            hssfCell.setCellValue(this.getDoubleValue(cell));
        }
        hssfCell.setCellStyle(this.getStyle(workbook, hssfCell, this.getDecimals(cell)));
    }
    
    /**
     * {@inheritDoc}
     */
    protected HSSFCellStyle getStyle(HSSFWorkbook workbook, HSSFCell hssfCell, int decimals) {
        String styleName = String.format(STYLE, decimals);
        for (short i = 0; i < workbook.getNumCellStyles(); i++) {
            HSSFCellStyle style = workbook.getCellStyleAt(i);
            if (styleName.equals(style.getUserStyleName())) {
                return style;
            }
        }
        HSSFCellStyle style = workbook.createCellStyle();
        style.cloneStyleFrom(hssfCell.getCellStyle());
        short dataFormat = HSSFDataFormat.getBuiltinFormat("0.0000%");
        style.setDataFormat(dataFormat);
        style.setUserStyleName(styleName);
        return style;
    }

    
}
